﻿#include "Sun.h"

Sun::Sun() {
	//调用init 动作初始化  
	this->init_sun_produce_animation();
	this->sun_init();
	sprite = Sprite::create();
	sprite->runAction(RepeatForever::create(sun_produce_animate));
	this->scheduleUpdate();
}

void Sun::sun_init() {
	sun_num = 50;
	sun_time = 0; //太阳保留时间
	is_delete = false; 
	is_produce = false; 
	time_count = 0;
} // 初始化函数


//修改系统的时间调度函数  
//参数 update_time 是系统调用该函数 的间隔时间
 void Sun::update(float update_time) {
	 //time_count += update_time;
	 if (judge_delete()) {
		 //run_sun_delete_animation();
		 this->unscheduleUpdate();
	 }
 } //调度函数

 // 精灵初始化函数  参1 ：图片名字  参2 ： 二维坐标  
void Sun::sprite_init(const Vec2& position_) {
	//sprite = Sprite::create("Sums/Sun3.png");
	 sprite->setPosition(position_); // 随机生成
} 
	
 // 创建太阳生成动画  
void Sun::init_sun_produce_animation() {
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Plants/Sun/Sun_default.plist"); //22 
	auto frames = getAnimation("Sun%d.png", 8); // 调用加载图片
	sprite = Sprite::createWithSpriteFrame(frames.front());
	auto animation1 = Animation::createWithSpriteFrames(frames, 1.0f / 8); // 缓存时间 约小则加载越快  
	sun_produce_animate = Animate::create(animation1);
}

//创建太阳收集动画  未完善
 void Sun::init_sun_collect_animation() {
	 sun_collect_animate = MoveTo::create(0.5f,Vec2(100,200));
 } 

 void Sun::run_sun_produce_animation() {
	 sprite->runAction(RepeatForever::create(sun_produce_animate));
 } // ̫运行太阳生成动画

 void Sun::run_sun_collect_animation() {
	 this->sprite->runAction(MoveTo::create(0.7f, Vec2(67, 560)));
	 auto seq = Sequence::createWithTwoActions(DelayTime::create(2.2f), RemoveSelf::create());
	 this->sprite->runAction(seq);
	 this->is_delete = true;
	 this->unscheduleUpdate();
 }

 void Sun::run_sun_delete_animation() {
	 auto seq = Sequence::createWithTwoActions(Blink::create(2.0, 5), RemoveSelf::create());
	 sprite->runAction(seq);
	 this->is_delete = true;
	 this->unscheduleUpdate();
 }
//设置为可删除
void Sun::set_is_delete() {
	is_delete = true;
}

//设置为可生成
void Sun::set_is_produce() {
	is_produce = true;
}

bool Sun::judge_prodece() {
	return is_produce;
} // 判断能否生成

bool Sun::judge_delete() {
	return is_delete;
} // 判断能否删除


Vector<SpriteFrame*> Sun::getAnimation(const char* format, int count) {
	auto spritecache = SpriteFrameCache::getInstance();
	Vector<SpriteFrame*> animFrames;
	char str[100];
	for (int i = 1; i <= count; i++) {
		sprintf(str, format, i);
		animFrames.pushBack(spritecache->getSpriteFrameByName(str));
	}
	return animFrames;
}